<template>
  <el-dialog
    title="上报工作日志"
    :visible.sync="dialogVisible"
    :modal="true"
    :appendToBody="true"
    width="88%"
    v-dialog-drag
    :close-on-click-modal="false"
    top="5vh"
    class='dialog-report-dis'
    :loading="saveloading"
    @close="handleClose()"
    @open="handleOpen()"
  >
    <div class="title-wrap">
      <PartitionTitle :title="form.dateTime" type='line'></PartitionTitle>
      <el-button type="text" class='to-history' @click="toHistory">查看历史日志</el-button>
    </div>

    <workLogItem
      v-if='isShowWork'
      :row='form'
      :isDisabled="false"
      mode="report"
      ref="workForm"
      @update="update"
    />
    <span slot="footer" class="dialog-footer">
      <el-button  @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">保 存</el-button>
    </span>

    <workHistoryLog
      :visible.sync="historyDialog.visible"
      :row = "historyDialog.row"
      :total = "historyDialog.total"
      :defaltSearch="queryParams"
    />
  </el-dialog>
</template>
<script>
  import {addOrUpdatedLog,getHistoryLog } from "@/api/workbench/index";
  import workLogItem from "./workLogItem";
  import workHistoryLog from "./workHistoryLog";
  import { mapState,mapGetters, mapMutations,mapActions } from "vuex";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: null
    }
  },

  data() {
    return {
      form: {
        dateTime:null,
        factoryDeptId:null,
        factoryDeptCode:null,
        workshopId:null,
        lineId:null,
        stationId:null,
        groupId:null,
        preLogId:null,
        processParamAdjust:null,
        equipmentOperationDes:null,
        hygieneCleanDes:null,
        notes:null,
        secureDes:null,
        qualityDes:null
      },
      queryParams:{
        factoryDeptCode:null,
        workshopCode:null,
        workshopId:null,
        lineId:null,
        stationId:null,
        workStationCode:null,
        searchDate:null
      },
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 5
      },
      saveloading:false,
      isShowWork:false,
      historyDialog:{
        visible: false,
        row:null,
        total:0
      }
    };
  },
  computed: {
    dialogVisible: {
      set(val) {
        this.$emit("update:visible", val)
      },
      get() {
        return this.visible
      }
    },
    ...mapGetters([
      'permitList',
      'realname'
    ]),
  },

  methods: {
    handleClose() {
      this.resetFormData();
      this.isShowWork = false;
      this.saveloading = false;
    },

    handleOpen() {
      this.resetFormData();
      if(this.permitList?.length){
        this.form.factoryDeptId = this.permitList[0]?.factoryDeptId || null;
        this.form.factoryDeptCode = this.permitList[0]?.factoryDeptCode || null;
        this.form.workshopId = this.permitList[0]?.workshopVos[0]?.workshopId || null;
        this.form.lineId = this.permitList[0]?.workshopVos[0]?.productionLineVos[0]?.lineId || null;
        this.form.stationId = this.permitList[0]?.workshopVos[0]?.productionLineVos[0]?.workStationVos[0]?.workStationId || null;

        this.queryParams.factoryDeptCode = this.permitList[0]?.factoryDeptId || null;
        this.queryParams.workshopCode = this.permitList[0]?.workshopVos[0]?.workshopCode || null;
        this.queryParams.workshopId = this.permitList[0]?.workshopVos[0]?.workshopId || null;
        this.queryParams.lineId = this.permitList[0]?.workshopVos[0]?.productionLineVos[0]?.lineId || null;
        this.queryParams.stationId = this.permitList[0]?.workshopVos[0]?.productionLineVos[0]?.workStationVos[0]?.workStationId || null;
        this.queryParams.workStationCode = this.permitList[0]?.workshopVos[0]?.productionLineVos[0]?.workStationVos[0]?.workStationCode || null;
      }
      this.form.dateTime = this.parseTime(new Date(),"{y}-{m}-{d}");
      this.isShowWork = true;
    },

    //上报日志
    handleSubmit() {
      const workForm = this.$refs["workForm"];
      if(!workForm.form.preLogId){
        this.msgError("当前产线不存在生产中的交接班记录")
        return
      }
      this.saveloading = true;
      workForm.$refs.ruleForm
      .validate((flag) => {
        if (flag) {
          const {
            dateTime,
            factoryDeptId,
            factoryDeptCode,
            workshopId,
            stationId,
            lineId,
            groupId,
            preLogId,
            processParamAdjust,
            hygieneCleanDes,
            equipmentOperationDes,
            notes,
            secureDes,
            qualityDes
          } = workForm.form
          const parmas = {
            dateTime,
            factoryDeptId,
            factoryDeptCode,
            workshopId,
            stationId,
            lineId,
            groupId,
            preLogId,
            processParamAdjust,
            hygieneCleanDes,
            equipmentOperationDes,
            notes,
            secureDes,
            qualityDes
          }
          addOrUpdatedLog(parmas).then(res => {
            if (res.code == 200) {
              this.msgSuccess("操作成功")
              this.dialogVisible = false
            } else {
              this.msgError(res.msg || "操作失败")
            }
            this.saveloading = false
          }).catch(() => {
            this.msgError("操作失败")
            this.saveloading = false
          })
        } else {
          this.saveloading = false
        }
      })
    },

    //查看历史日志
    toHistory(){
      const { pageNum, pageSize } = this.pager;
      const parmas = {
        query:{
          workshopId:this.queryParams.workshopId,
          stationId:this.queryParams.stationId,
          lineId:this.queryParams.lineId,
          searchDate:null
        },
        pageNum,
        pageSize
      }
      getHistoryLog(parmas)
      .then((res) => {
        if(res.code == 200 && res?.rows?.length){
          this.historyDialog.visible = true;
          this.historyDialog.row = res?.rows || []
          this.historyDialog.total = res?.total;
          this.saveloading = false;
        }else{
          this.msgError("暂无历史日志")
          this.historyDialog.row = [];
          this.historyDialog.total = 0;
          this.historyDialog.visible = false;
          this.saveloading = false;
        }
      })
      .catch(() => {
        this.msgError("暂无历史日志")
        this.historyDialog.row = []
        this.historyDialog.visible = false;
        this.historyDialog.total = 0;
        this.saveloading = false;
      });
    },

    update(item,type,flag){
      if(type == "workshop"){
        if(!flag){//手动修改车间的值
          this.queryParams.lineId = null;
          this.queryParams.stationId = null;
          this.queryParams.workStationCode = null;
        }
        this.queryParams.workshopCode = item?.workshopCode || null;
        this.queryParams.workshopId = item?.workshopId || null;
      }
      else if(type == "line"){
        if(!flag){
          this.queryParams.stationId = null;
          this.queryParams.workStationCode = null;
        }
        this.queryParams.lineId = item?.lineId || null;
      }
      else if(type == "station"){
        this.queryParams.stationId = item?.workStationId || null;
        this.queryParams.workStationCode = item?.workStationCode || null;
      }
    },

    resetFormData() {
      this.form = {
        dateTime:null,
        factoryDeptId:null,
        factoryDeptCode:null,
        workshopId:null,
        lineId:null,
        stationId:null,
        groupId:null,
        groupName:null,
        classesName:null,
        reportName:null,
        startTime:null,
        endTime:null,
        preLogId:null,
        processParamAdjust:null,
        hygieneCleanDes:null,
        notes:null,
        equipmentOperationDes:null,
        secureDes:null,
        qualityDes:null
      };
      this.queryParams = {
        factoryDeptCode:null,
        workshopCode:null,
        workshopId:null,
        lineId:null,
        stationId:null,
        workStationCode:null,
        searchDate:null
      };
    },
  },
  components:{
    workHistoryLog,
    workLogItem
  }
};
</script>
<style lang="scss" scoped>
.el-form {
  .el-date-editor.el-input,
  .el-select {
    width: 100%;
  }
}
.title-wrap{
  display: flex;
  align-items: center;
  .to-history{
    margin-bottom: 8px;
  }
  .block-title{
    margin-bottom: 8px;
  }
}
::v-deep.dialog-report-dis{
  .el-dialog {
    .el-dialog__body{
      padding:8px 1.25rem;
    }
  }
}

</style>
